Scroll Progress Bar

Keywords

C++ has a set of keywords that serve as reserved words and have specific meanings in the language. These keywords cannot be used as identifiers (names for variables, functions, classes, etc.) because they are part of the language's syntax. Here is a list of C++ keywords as of my last knowledge update in September 2021:

keywords
alignas alignof and and_eq
asm atomic_cancel atomic_commit atomic_noexcept
auto bitand bitor bool
break case catch char
char8_t char16_t char32_t class
compl concept const consteval
constexpr const_cast continue co_await
co_return co_yield decltype default
delete do double dynamic_cast
else enum explicit export
extern false float for
friend goto if import
inline int long module
mutable namespace new noexcept
not not_eq nullptr operator
or or_eq private protected
public register reinterpret_cast requires
return short signed sizeof
static static_assert static_cast struct
switch synchronized template this
thread_local throw true try
typedef typeid typename union
unsigned using virtual void
volatile wchar_t while xor

Certainly! Here's an explanation and a simple example for each of the keywords in C++. Please note that this list is based on C++ standards up to C++17.

alignas: Specifies alignment requirement for a variable or type. This keyword ensures that a variable or type is aligned in memory according to the specified alignment.


alignas(16) int alignedInt; // Aligns 'alignedInt' to a 16-byte boundary.

alignof: Determines the alignment requirement of a type or expression. It returns the alignment requirement in bytes.


alignof(int); // Returns the alignment requirement of an 'int' in bytes.

and: Logical AND operator. It returns true if both operands are true.


if (x > 0 and y > 0) { /* Both x and y are greater than 0. */ }

and_eq: Bitwise AND assignment operator. It performs a bitwise AND between two values and assigns the result to the left operand.


x and_eq y; // Equivalent to 'x &= y;'

asm: Used for inline assembly language code. It allows you to include assembly instructions within C++ code.


asm("movl $1, %eax"); // Inline assembly to move 1 into the 'eax' register.

atomic_cancel: Specifies a cancel region for atomic operations. This is related to C++20's atomic cancelation support, used for handling exceptions in atomic operations. atomic_commit: Specifies a commit region for atomic operations. This is related to C++20's atomic commit support, used for transactions in atomic operations. atomic_noexcept: Specifies that atomic operations are noexcept. It's used to indicate that atomic operations won't throw exceptions. auto: Specifies automatic storage duration and infers the data type based on the assigned value.


auto x = 42; // 'x' is automatically inferred as int.

bitand: Bitwise AND operator. It performs a bitwise AND operation between two values.


int result = x bitand y; // 'result' stores the bitwise AND of 'x' and 'y'.

bitor: Bitwise OR operator. It performs a bitwise OR operation between two values.


int result = x bitor y; // 'result' stores the bitwise OR of 'x' and 'y'.

bool: Represents a Boolean type, which can have values true or false.


bool isValid = true; // 'isValid' is a Boolean variable.

break: Used to exit a loop or switch statement prematurely.


for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exit the loop when 'i' is 5.
    }
}

case: Used in a switch statement to define different cases for different values.


switch (day) {
    case 1:
        // Code for Monday
        break;
    case 2:
        // Code for Tuesday
        break;
    default:
        // Code for other days
        break;
}

catch: Used in exception handling to catch and handle exceptions.


try {
    // Code that may throw an exception
}
catch (const std::exception& e) {
    // Handle the exception
}

char: Represents a character type. It can store a single character.


char letter = 'A'; // 'letter' stores the character 'A'.

char8_t: Represents an 8-bit character type. Introduced in C++20, used for UTF-8 character encoding.


char8_t utf8Char = u8'€'; // 'utf8Char' stores a UTF-8 character.

char16_t: Represents a 16-bit character type. Introduced in C++11, used for UTF-16 character encoding.


char16_t utf16Char = u'€'; // 'utf16Char' stores a UTF-16 character.

char32_t: Represents a 32-bit character type. Introduced in C++11, used for UTF-32 character encoding.


char32_t utf32Char = U'€'; // 'utf32Char' stores a UTF-32 character.

class: Used to define a class, which is a blueprint for creating objects.


class Person {
public:
    std::string name;
    int age;
};

compl: Bitwise NOT operator. It performs a bitwise negation of a value.


int result = compl x; // 'result' stores the bitwise negation of 'x'.

concept: Introduced in C++20, it defines constraints on template arguments.


template
concept Integral = std::is_integral::value;

const: Used to declare a constant variable or pointer. It indicates that the value cannot be modified.


const int maxCount = 100; // 'maxCount' is a constant.

const_cast: Used for typecasting away the const qualifier from a variable.


const int x = 42;
int* ptr = const_cast(&x); // Remove 'const' qualifier to modify 'x'.

continue: Used to skip the rest of the current iteration of a loop and move to the next iteration.


for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue; // Skip even numbers.
    }
    // Code here executes for odd 'i'.
}

co_await: Introduced in C++20, it is used in coroutines for asynchronous operations.


await fetchDataAsync(); // Await an asynchronous operation.

co_return: Introduced in C++20, it is used in coroutines to return a value from a coroutine.


co_return result; // Return 'result' from a coroutine.

co_yield: Introduced in C++20, it is used in coroutines to yield a value.


co_yield value; // Yield 'value' from a coroutine.

decltype: Determines the data type of an expression at compile-time.


decltype(x + y) sum; // 'sum' has the same data type as the result of 'x + y'.

default: Used in switch statements to specify the default case.


switch (choice) {
    case 1:
        // Code for case 1
        break;
    default:
        // Default case code
        break;
}

delete: Used to delete a function or operator, preventing its use.


class MyClass {
public:
    void foo() = delete; // Deleting the 'foo' function.
};

do: Used to create a do-while loop, which executes a block of code at least once.


do {
    // Code here
} while (condition);

double: Represents a double-precision floating-point type.


double pi = 3.14159; // 'pi' stores a floating-point value.

dynamic_cast: Used for dynamic type casting in polymorphic classes.


class Base { /* ... */ };
class Derived : public Base { /* ... */ };
Base* ptr = new Derived;
Derived* derivedPtr = dynamic_cast(ptr);

else: Used in conditional statements to specify an alternative code block.


if (condition) {
    // Code if the condition is true
} else {
    // Code if the condition is false
}

enum: Used to define an enumeration type, which represents a set of named integer values.


enum Color { Red, Green, Blue };
Color selectedColor = Green;

explicit: Used to declare an explicit constructor to prevent implicit type conversions.


class MyString {
public:
    explicit MyString(int length); // Explicit constructor
};

export: Reserved for future use. Intended for use in module systems (C++20 and later). extern: Used to declare a global variable or function that is defined in another source file.


extern int globalVar; // Declaration of a global variable.

false: Represents the Boolean value false.


bool isValid = false; // 'isValid' is initialized as 'false'.

float: Represents a single-precision floating-point type.


float temperature = 24.5; // 'temperature' stores a floating-point value.

for: Used to create a for loop, which iterates a specified number of times.


for (int i = 0; i < 10; i++) {
    // Code inside the loop
}

friend: Used to declare a function or class as a friend, allowing it to access private members of another class.


class MyClass {
    friend void friendFunction(); // Declaring a friend function.
};

goto: Used to transfer control to a labeled statement.


goto labelName;
// ...
labelName:
// Code here

if: Used to create conditional statements to execute code based on a condition.


if (condition) {
    // Code executed if the condition is true
}

import: Reserved for future use. Intended for use in module systems (C++20 and later). inline: Used to suggest to the compiler to inline a function for optimization purposes.


inline int add(int a, int b) {
    return a + b;
}

int: Represents an integer data type.


int count = 42; // 'count' stores an integer value.

long: Represents a long integer data type.


long bigNumber = 1234567890L; // 'bigNumber' stores a long integer value.

module: Reserved for future use. Intended for use in module systems (C++20 and later). mutable: Used to declare a member of a class as mutable, allowing it to be modified in a const member function.


class MyClass {
public:
    mutable int counter;
    void increment() const {
        counter++; // 'counter' can be modified in a 'const' function.
    }
};

namespace: Used to declare a namespace, which is a named scope for organizing code.


namespace Math {
    int add(int a, int b) {
        return a + b;
    }
}

new: Used for dynamic memory allocation, creating objects on the heap.


int* dynamicInt = new int; // Dynamically allocate an integer.

noexcept: Specifies that a function does not throw exceptions.


void safeFunction() noexcept {
    // Function code that won't throw exceptions.
}

not: Logical NOT operator. It returns true if the operand is false, and false if the operand is true.


if (not isValid) { /* Code if 'isValid' is false */ }

not_eq: Bitwise NOT assignment operator. It performs a bitwise NOT operation and assigns the result to the left operand.


x not_eq y; // Equivalent to 'x != y;'

nullptr: Represents a null pointer, used to indicate that a pointer does not point to any object.


int* ptr = nullptr; // 'ptr' is a null pointer.

operator: Used to define custom operators for user-defined types.


class Complex {
public:
    Complex operator+(const Complex& other) {
        // Custom '+' operator for Complex numbers.
    }
};

or: Logical OR operator. It returns true if at least one operand is true.


if (x > 0 or y > 0) { /* At least one of x or y is greater than 0. */ }

or_eq: Bitwise OR assignment operator. It performs a bitwise OR operation between two values and assigns the result to the left operand.


x or_eq y; // Equivalent to 'x |= y;'

private: Access specifier used in class definitions to specify private members. Private members are accessible only within the class.


class MyClass {
private:
    int secretValue;
};

protected: Access specifier used in class definitions to specify protected members. Protected members are accessible within the class and derived classes.


class MyBaseClass {
protected:
    int protectedValue;
};

public: Access specifier used in class definitions to specify public members. Public members are accessible from anywhere.


class MyClass {
public:
    int publicValue;
};

register: Suggests to the compiler to store a variable in a register for optimization. Modern compilers often ignore this keyword.


register int counter; // Suggest to store 'counter' in a register.

reinterpret_cast: Used for low-level type casting between unrelated types.


int x = 42;
float* floatPtr = reinterpret_cast(&x); // Type casting 'int' to 'float*'.

requires: Introduced in C++20, it defines requirements on template arguments.


template
requires std::is_integral::value
T add(T a, T b) {
    return a + b;
}

return: Used to return a value from a function.


int add(int a, int b) {
    return a + b;
}

short: Represents a short integer data type.


short smallNumber = 123; // 'smallNumber' stores a short integer value.

signed: Specifies that a data type can store both positive and negative values.


signed int temperature = -10; // 'temperature' can store negative values.

sizeof: Used to determine the size in bytes of a data type or object.


size_t size = sizeof(int); // 'size' stores the size of an 'int' in bytes.

static: Used to define static members of a class, which are shared among all instances of the class.


class MyClass {
public:
    static int instanceCount;
};
int MyClass::instanceCount = 0; // Initialize the static member.

static_assert: Used for compile-time assertions.


static_assert(sizeof(int) == 4, "int must be 4 bytes"); // Compile-time check.

static_cast: Used for type casting between related types in a safe manner.


double x = 3.14;
int intValue = static_cast(x); // Safely convert 'double' to 'int'.

struct: Used to define a structure, which is similar to a class but has default public members.


struct Point {
    int x;
    int y;
};

switch: Used to create a switch statement for branching based on a value.


switch (choice) {
    case 1:
        // Code for case 1
        break;
    case 2:
        // Code for case 2
        break;
    default:
        // Code for other cases
        break;
}

synchronized: Reserved for future use. Intended for use in concurrent programming. template: Used to define template classes and functions for generic programming.


template
T add(T a, T b) {
    return a + b;
}

this: A pointer that refers to the current object within a member function of a class.


class MyClass {
public:
    void printAddress() {
        std::cout << "Address of 'this': " << this << std::endl;
    }
};

thread_local: Specifies that a variable has thread-local storage duration.


thread_local int threadLocalVar; // 'threadLocalVar' is thread-local.

throw: Used to throw an exception in exception handling.


if (errorCondition) {
    throw MyException("An error occurred.");
}

true: Represents the Boolean value true.


bool isValid = true; // 'isValid' is initialized as 'true'.

try: Used to define a block of code where exceptions may be thrown and caught.


try {
    // Code that may throw exceptions
}

typedef: Used to create a type alias, giving an existing data type a new name.


typedef int Age; // 'Age' is a type alias for 'int'.
Age personAge = 30; // 'personAge' is of type 'Age'.

typeid: Used to obtain information about an object's type at runtime.


const std::type_info& typeInfo = typeid(object); // Get type information.

typename: Used in template programming to specify that a dependent name is a type.


template 
void printType() {
    typename T::type variable; // 'typename' indicates 'T::type' is a type.
}

union: Used to define a union, which is a data structure that can hold different types of data.


union Data {
    int i;
    float f;
};

unsigned: Specifies that a data type can store only non-negative values.


unsigned int count = 42; // 'count' can store only non-negative values.

using: Used to create a type alias or bring a namespace into the current scope.


using Score = int; // 'Score' is a type alias for 'int'.
using namespace std; // Bring the 'std' namespace into the current scope.

virtual: Used to declare virtual functions in base classes for polymorphism.


class Base {
public:
    virtual void foo() {
        // Virtual function
    }
};

void: Represents a lack of data type. It is often used to indicate that a function does not return a value.


void printMessage() {
    // Function with no return value
}

Please note that C++ standards evolve, and new keywords may be introduced in later versions of the language. Additionally, some keywords like export, import, and synchronized were reserved for future use at the time of my last knowledge update (C++17), and their specific usage may have evolved in newer standards like C++20 and beyond.


question


answer

question2


answer2